//
//  TaskListCell.swift
//  SBSafetyApp
//
//  Created by Aimeow on 2022/11/16.
//
// https://pic6.58cdn.com.cn/nowater/webim/big/n_v240746a5286164c5ba58f1e7c3c0ba1a7.png

import Foundation
import UIKit

enum TaskStateEnum: String {
    case dwc = "0"
    case aqwc = "1"
    case cqwc = "2"
    case cqiwc = "3"
    case cqiwwc = "4"
    case wwc = "5"
}

class TaskListModal: NSObject {
    var id: Int64 = 0
    var title: String = ""
    var dateStr: String = ""
    var state: TaskStateEnum
    var stateCount: Int = -1

    init(id: Int64, title: String, dateStr: String, state: TaskStateEnum, stateCount: Int) {
        self.id = id
        self.title = title
        self.dateStr = dateStr
        self.state = state
        self.stateCount = stateCount
    }
}

class TaskListCellView: UIControl {
    lazy var statusIV: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage.init(named: "ic_task_wwc")
        imageView.contentMode = UIView.ContentMode.scaleAspectFit
        return imageView;
    }()
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.hex("#333333")
        label.font = UIFont.systemFont(ofSize: 17)
        return label
    }()

    lazy var dateIconImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage.init(named: "ic_clock")
        imageView.contentMode = UIView.ContentMode.scaleAspectFit
        return imageView;
    }()

    lazy var dateLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.hex("#BABABA")
        label.font = UIFont.systemFont(ofSize: 14)
        label.adjustsFontSizeToFitWidth = true
        return label
    }()

    lazy var stateTextLabel: UILabel = {
        let label = UILabel()
        label.textColor = .hex("#B6B6B6")
        label.font = .systemFont(ofSize: 15)
        label.textAlignment = .right
        return label
    }()

    lazy var indicatorImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(systemName: "chevron.right")
        imageView.tintColor = UIColor.hex("#A5A5A5")
        imageView.contentMode = .scaleAspectFit
        return imageView
    }()

    lazy var line: UIView = {
        let view = UIView.createLine()
        return view
    }()

    var formatter: DateFormatter = {
        let formatter = DateFormatter()
        formatter.timeZone = TimeZone.current
        formatter.dateFormat = "yyyy-MM-dd HH:mm"
        return formatter
    }()

    init() {
        super.init(frame: .zero)
        backgroundColor = UIColor.white
        setupUI()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override var isHighlighted: Bool {
        didSet {
            if isHighlighted {
                self.backgroundColor = .hex("#ECECEC")
            } else {
                self.backgroundColor = .white
            }
        }
    }

    func buildData(data: LzglbaListModel?) {
        titleLabel.text = data?.rwmc
        dateLabel.text = "\(data?.rwzxkssj ?? "")~\(data?.rwzxssj ?? "")"
        
        let rwzt = TaskStateEnum(rawValue: data?.rwzt ?? "0")!
        switch rwzt {
        case .dwc: // 0
            stateTextLabel.text = "待完成"
            stateTextLabel.textColor = UIColor.hex("#FF0000")
            statusIV.image = UIImage(named: "ic_task_wwc")
        case .aqwc: // 1
            stateTextLabel.text = "按期完成"
            stateTextLabel.textColor = UIColor.hex("#B6B6B6")
        case .cqwc: // 2
            stateTextLabel.text = "超前完成"
            stateTextLabel.textColor = UIColor.hex("#B6B6B6")
        case .cqiwc: // 3
            stateTextLabel.text = "超期完成"
            stateTextLabel.textColor = UIColor.hex("#B6B6B6")
        case .cqiwwc: // 4
            stateTextLabel.text = "超期未完成"
            stateTextLabel.textColor = UIColor.hex("#FF0000")
            statusIV.image = UIImage(named: "ic_task_wwc")
        case .wwc: // 5
            stateTextLabel.text = "未完成"
            stateTextLabel.textColor = UIColor.hex("#FF0000")
            statusIV.image = UIImage(named: "ic_task_wwc")
        }
        
        if rwzt == .aqwc || rwzt == .cqwc || rwzt == .cqiwc {
            if data?.yczt == "0" {
                statusIV.image = UIImage(named: "ic_task_zc")
            } else if data?.yczt == "1" {
                statusIV.image = UIImage(named: "ic_task_yc")
            }
        }
    }

    func buildModels(data: RwwcqkModel) -> Void {
        if let taskName = data.taskName {
            titleLabel.text = taskName
        }

        if (data.wcTotal ?? 0) < (data.total ?? 0) {
            stateTextLabel.text = "未完成(\(data.wwcTotal ?? 0))"
            stateTextLabel.textColor = UIColor.hex("#FF0000")
            statusIV.image = UIImage(named: "ic_task_wwc")
        } else {
            stateTextLabel.text = "已完成"
            stateTextLabel.textColor = UIColor.hex("#B6B6B6")
            statusIV.image = UIImage(named: "ic_task_zc")
        }
        dateLabel.text = "\(data.startTime ?? "") - \(data.endTime ?? "")"
    }

    func buildPercentModels(data: WdwcqkPercentModel) {
        if let taskName = data.taskName {
            titleLabel.text = taskName
        }
        dateLabel.text = "\(data.startTime ?? "") - \(data.endTime ?? "")"
        
        let rwzt = TaskStateEnum(rawValue: data.status ?? "0")!
        switch rwzt {
        case .dwc: // 0
            stateTextLabel.text = "未完成(\(data.wwcTotal ?? 0))"
            stateTextLabel.textColor = UIColor.hex("#FF0000")
            statusIV.image = UIImage(named: "ic_task_wwc")
        case .aqwc: // 1
            stateTextLabel.text = "按期完成"
            stateTextLabel.textColor = UIColor.hex("#B6B6B6")
            statusIV.image = UIImage(named: "ic_task_zc")
        case .cqwc: // 2
            stateTextLabel.text = "超前完成"
            stateTextLabel.textColor = UIColor.hex("#B6B6B6")
            statusIV.image = UIImage(named: "ic_task_zc")
        case .cqiwc: // 3
            stateTextLabel.text = "超期完成"
            stateTextLabel.textColor = UIColor.hex("#B6B6B6")
            statusIV.image = UIImage(named: "ic_task_zc")
        case .cqiwwc: // 4
            stateTextLabel.text = "超期未完成"
            stateTextLabel.textColor = UIColor.hex("#FF0000")
            statusIV.image = UIImage(named: "ic_task_wwc")
        case .wwc: // 5
            stateTextLabel.text = "未完成(\(data.wwcTotal ?? 0))"
            stateTextLabel.textColor = UIColor.hex("#FF0000")
            statusIV.image = UIImage(named: "ic_task_wwc")
        }
    }
    
    func buildModels(data: BaglRwwcqkModel) -> Void {
        if let taskName = data.taskName {
            titleLabel.text = taskName
        }

        if (data.wcTotal == 0) || (data.wcTotal ?? 0) < (data.total ?? 0) {
            stateTextLabel.text = "未完成(\(data.wwcTotal ?? 0))"
            stateTextLabel.textColor = UIColor.hex("#FF0000")
            statusIV.image = UIImage(named: "ic_task_wwc")
        } else {
            stateTextLabel.text = "已完成"
            stateTextLabel.textColor = UIColor.hex("#B6B6B6")
            statusIV.image = UIImage(named: "ic_task_zc")
        }
        dateLabel.text = ""
        dateIconImageView.isHidden = true

        titleLabel.snp.remakeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(statusIV.snp.right).offset(12)
        }
        
//        dateLabel.text = "\(data.startTime ?? "") - \(data.endTime ?? "")"
    }
    
    func buildData(data: BaglSecurityInfoRwjlModel) {
        titleLabel.text = data.rwmc
        dateLabel.text = "\(data.rwkssj ?? "")~\(data.rwjssj ?? "")"
        
        let rwzt = TaskStateEnum(rawValue: data.rwzt ?? "0")!
        switch rwzt {
        case .dwc: // 0
            stateTextLabel.text = "待完成"
            stateTextLabel.textColor = UIColor.hex("#FF0000")
            statusIV.image = UIImage(named: "ic_task_wwc")
        case .aqwc: // 1
            stateTextLabel.text = "按期完成"
            stateTextLabel.textColor = UIColor.hex("#B6B6B6")
        case .cqwc: // 2
            stateTextLabel.text = "超前完成"
            stateTextLabel.textColor = UIColor.hex("#B6B6B6")
        case .cqiwc: // 3
            stateTextLabel.text = "超期完成"
            stateTextLabel.textColor = UIColor.hex("#B6B6B6")
        case .cqiwwc: // 4
            stateTextLabel.text = "超期未完成"
            stateTextLabel.textColor = UIColor.hex("#FF0000")
            statusIV.image = UIImage(named: "ic_task_wwc")
        case .wwc: // 5
            stateTextLabel.text = "未完成"
            stateTextLabel.textColor = UIColor.hex("#FF0000")
            statusIV.image = UIImage(named: "ic_task_wwc")
        }
        
        if rwzt == .aqwc || rwzt == .cqwc || rwzt == .cqiwc {
            if data.yczt == "0" {
                statusIV.image = UIImage(named: "ic_task_zc")
            } else if data.yczt == "1" {
                statusIV.image = UIImage(named: "ic_task_yc")
            }
        }
    }

    func setupUI() {
        addSubview(statusIV)
        addSubview(titleLabel)
        addSubview(dateIconImageView)
        addSubview(dateLabel)
        addSubview(stateTextLabel)
        addSubview(indicatorImageView)
        addSubview(line)
        
        statusIV.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.centerY.equalTo(self)
            make.width.height.equalTo(34)
        }

        titleLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(15)
            make.left.equalTo(statusIV.snp.right).offset(12)
            make.right.equalTo(stateTextLabel.snp.left).offset(-20)
        }

        dateIconImageView.snp.makeConstraints { make in
            make.left.equalTo(statusIV.snp.right).offset(12)
            make.height.equalTo(12)
            make.width.equalTo(12)
            make.top.equalTo(titleLabel.snp.bottom).offset(7)
        }

        dateLabel.snp.makeConstraints { make in
            make.centerY.equalTo(dateIconImageView)
            make.left.equalTo(dateIconImageView.snp.right).offset(5)
            make.right.equalTo(stateTextLabel.snp.left).offset(-20)
        }

        indicatorImageView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-10)
            make.width.equalTo(10)
        }

        stateTextLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(indicatorImageView.snp.left).offset(-10)
            make.height.equalTo(14)
        }

        line.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.height.equalTo(0.5)
            make.bottom.equalToSuperview()
        }
    }
}

class TaskListCell: UITableViewCell {
    lazy var taskCell: TaskListCellView = {
        let view = TaskListCellView()
        view.isUserInteractionEnabled = false
        return view
    }()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = UIColor.white
        contentView.addSubview(taskCell)
        taskCell.snp.makeConstraints { make in
            make.left.right.top.bottom.equalTo(contentView)
        }
    }
    
    override func setHighlighted(_ highlighted: Bool, animated: Bool) {
        if highlighted {
            taskCell.backgroundColor = .hex("#ECECEC")
        } else {
            taskCell.backgroundColor = .white
        }
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func buildData(data: LzglbaListModel?) {
        taskCell.buildData(data: data)
    }
    
    func buildData(data: BaglSecurityInfoRwjlModel) {
        taskCell.buildData(data: data)
    }

    func buildModels(data: RwwcqkModel) -> Void {
        taskCell.buildModels(data: data)
    }

    func buildPercentModels(data: WdwcqkPercentModel) {
        taskCell.buildPercentModels(data: data)
    }
}
